// pages/charge/w_charge/w_charge.js

const app = getApp() // 获取应用实例
const util = require('../../../utils/util')
import { getWAccountList, postUnbindWater } from '../../../utils/http/api'

Page({

  data: {
    // 缴费户号
    account_num: '',
    // 户号列表
    account_list: [],
  },

  // 点击缴费按钮，先验证是否输入户号
  toCharge: function (e) {
    if (util.isNull(this.data.account_num)) {
      wx.showToast({ title: '请输入缴费户号', icon: 'none' })
      return
    }
    // 判断当前登录用户是否绑定当前输入的户号
    let isBind = false
    if (this.data.account_list) {
      this.data.account_list.map(item => {
        if (this.data.account_num == item.water) {
          isBind = true
        }
      })
    }
    let url = '/pages/charge/w_charge_2/w_charge_2?account_num=' + this.data.account_num
    if (isBind) {
      util.navigateTo(url)
    } else {
      wx.showModal({
        title: '温馨提示', content: `检测到您可能暂未绑定该户号水表,是否确定为其缴费?`,
        success(res) {
          if (res.confirm) {
            util.navigateTo(url)
          }
        }
      })
    }
  },

  // 点击户号直接缴费
  toChargeByItem: function (e) {
    let index = e.currentTarget.dataset.index
    let account_num = this.data.account_list[index].water
    let url = '/pages/charge/w_charge_2/w_charge_2?account_num=' + account_num
    util.navigateTo(url)
  },

  // 点击添加常用水表 绑定水表
  onAddCydb() {
    let that = this
    let url = '/pages/charge/w_charge_cydb/w_charge_cydb'
    // util.navigateTo(url)
    wx.navigateTo({
      url,
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        onBindOk(data) {
          if (data) {// 收到水表绑定成功的消息
            that.reqAccountList()
          }
        }
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { data: 'test' })
      }
    })

  },

  // 监听缴费户号输入框
  onInput(e) {
    // 监听输入的缴费户号，赋值给记录变量
    this.setData({ account_num: e.detail.value, })
  },

  // 取消常用水表
  onCancelCydb(e) {
    let that = this
    wx.showModal({
      title: '提示', content: '是否取消绑定该水表？',
      success(res) {
        if (res.confirm) {
          let index = e.currentTarget.dataset.index
          const water = that.data.account_list[index].water
          that.reqUnbindWater(water)
        }
      }
    })
  },

  // 接口:取消绑定水表号 
  async reqUnbindWater(water) {
    wx.showLoading({ title: '处理中', mask: true, })
    let { code, msg, data } = await postUnbindWater({ water, token: app.gl.token, })
    // console.log(`reqUnbindWater...code=${code},msg=${msg},data=`, data)
    wx.showModal({ title: '提示', content: msg, })
    if (code == '1') {
      this.reqAccountList()
    }
  },

  // 请求水费户号列表
  async reqAccountList() {
    wx.showLoading({ title: '处理中', mask: true, })
    let { code, msg, data } = await getWAccountList({ page: '1', limit: '999999', token: app.gl.token, })
    // console.log(`reqAccountList...code=${code},msg=${msg},data=`, data)
    wx.stopPullDownRefresh()
    if (code == '1') {
      this.setData({ account_list: data })
    } else {
      wx.showToast({ title: msg, icon: 'none' })
    }
  },

  // 生命周期函数--监听页面加载
  onLoad: function (options) {
    this.reqAccountList()
  },

  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh() {
    this.reqAccountList()
  },

})